package com.zj.service;

import com.zj.web.bean.MyUserDTO;
import com.zj.web.bean.OrderMessageDTO;
import com.zj.web.bean.OrdersDTO;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.io.StringWriter;
import java.time.LocalDateTime;
import java.util.Date;

@Component
public class VelocityTemplateService {
    @Autowired
    private VelocityEngine velocityEngine; //模板引擎
    @Autowired
    private VelocityContext velocityContext; //模板上下文

    //用户登录成功模板
    @Autowired
    @Qualifier("userLoginSuccessTemplate")  //使用Qualifier明确要注入的模板
    private Template userLoginSuccessTemplate; //用户登录成功模板

    //用户登录
    public String userLoginSuccessInfo(MyUserDTO myUserDTO){
        velocityContext.put("user",myUserDTO);
        velocityContext.put("currentTime",new Date());
        StringWriter stringWriter=new StringWriter();
        userLoginSuccessTemplate.merge(velocityContext,stringWriter);
        return stringWriter.toString();  //返回真正要发送的邮件内容
    }

    //用户注册发送验证码模板
    @Autowired
    @Qualifier("registerVerifyCodeTemplate")  //使用Qualifier明确要注入的模板
    private Template registerVerifyCodeTemplate; //用户注册发送验证码模板

    //用户注册发送验证码
    public String registerVerifyCodeInfo(MyUserDTO myUserDTO){
        velocityContext.put("user",myUserDTO);
        LocalDateTime expireTime = LocalDateTime.now().plusMinutes(5);
        velocityContext.put("expireTime", expireTime);
        velocityContext.put("currentTime",new Date());
        StringWriter stringWriter=new StringWriter();
        registerVerifyCodeTemplate.merge(velocityContext,stringWriter);
        return stringWriter.toString();  //返回真正要发送的邮件内容
    }

    //订单成功模板
    @Autowired
    @Qualifier("orderSuccessTemplate")  //使用Qualifier明确要注入的模板
    private Template orderSuccessTemplate; //订单成功模板

    //订单成功
    public String orderSuccessInfo(OrderMessageDTO orderMessageDTO){
        velocityContext.put("order",orderMessageDTO);
        velocityContext.put("currentTime",new Date());
        StringWriter stringWriter=new StringWriter();
        orderSuccessTemplate.merge(velocityContext,stringWriter);
        return stringWriter.toString();  //返回真正要发送的邮件内容
    }
    //订单状态更新模板
    @Autowired
    @Qualifier("orderStatusUpdateTemplate")  //使用Qualifier明确要注入的模板
    private Template orderStatusUpdateTemplate; //订单状态更新模板

    //订单状态更新
    public String orderStatusSuccessInfo(OrderMessageDTO orderMessageDTO){
        velocityContext.put("order",orderMessageDTO);
        velocityContext.put("currentTime",new Date());
        StringWriter stringWriter=new StringWriter();
        orderStatusUpdateTemplate.merge(velocityContext,stringWriter);
        return stringWriter.toString();  //返回真正要发送的邮件内容
    }
}
